package 剑指Offer.easy;

public class 删除链表的结点 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    class Solution2 {
        public ListNode deleteNode(ListNode head, int val) {
            ListNode root = new ListNode(0);//保存头结点
            root.next = head;
            ListNode tmp = null;//保存上一个结点
            ListNode cur = root;//保存当前结点
            while (cur.next != null) {
                tmp = cur;
                cur = cur.next;
                if (cur.val == val) {
                    tmp.next = cur.next;
                    break;
                }
            }
            return root;
        }
    }

    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            ListNode root = new ListNode(0);//保存头结点
            root.next = head;
            ListNode tmp = root;//保存上一个结点
            while (head != null) {
                if (head.val == val) {
                    tmp.next = head.next;
                    break;
                }
                tmp = head;
                head = head.next;
            }

            return root.next;
        }
    }
}
